package xyz.playedu.api.controller.backend;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.utils.DateUtils;
import xyz.playedu.common.types.JsonResponse;
import xyz.playedu.common.types.paginate.DeviceNamePaginateFiler;
import xyz.playedu.common.types.paginate.DeviceTypePaginateFiler;
import xyz.playedu.common.types.paginate.PaginationResult;
import xyz.playedu.inspection.domain.DeviceNameDict;
import xyz.playedu.inspection.domain.DeviceTypeDict;
import xyz.playedu.inspection.service.DeviceNameDictService;
import xyz.playedu.inspection.service.DeviceTypeDictService;

import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/backend/v1/devicename")
public class DeviceNameDictController {

    @Autowired
    private DeviceNameDictService deviceNameDictService;

    @Autowired
    private DeviceTypeDictService deviceTypeDictService;

    @PostMapping("/create")
    public JsonResponse create(@RequestBody DeviceNameDict deviceNameDict) {
        HashMap<String, Object> data = new HashMap<>();
        deviceNameDict.setCreateTime(DateUtils.getNowDate());
        data.put("data", deviceNameDictService.save(deviceNameDict));
        return JsonResponse.data(data);
    }

    @GetMapping("/getList")
    public JsonResponse getList(@RequestParam HashMap<String, Object> params){
        Integer page = MapUtils.getInteger(params, "page", 1);
        Integer size = MapUtils.getInteger(params, "size", 10);

        String deviceName = MapUtils.getString(params, "deviceName");
        DeviceNamePaginateFiler filer = new DeviceNamePaginateFiler();
        filer.setDeviceName(deviceName);

        PaginationResult<DeviceNameDict> result = deviceNameDictService.paginate(page, size, filer);

        HashMap<String, Object> data = new HashMap<>();
        data.put("data",result.getData());
        data.put("total",result.getTotal());

        return JsonResponse.data(data);
    }
    @GetMapping("/getDeviceTypeById/{id}")
    public JsonResponse getDeviceTypeById(@PathVariable(name="id") Long id){
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",deviceNameDictService.getDeviceTypeById(id));
        return JsonResponse.data(map);
    }

    @PostMapping("/updateDeviceTypeById")
    public JsonResponse updateDeviceTypeById(@RequestBody DeviceNameDict deviceNameDict) {
        HashMap<String, Object> data = new HashMap<>();
        deviceNameDict.setUpdateTime(DateUtils.getNowDate());
        data.put("data", deviceNameDictService.updateById(deviceNameDict));
        return JsonResponse.data(data);
    }

    @GetMapping("/delInfoById/{id}")
    public JsonResponse delInfoById(@PathVariable(name="id") Long id){
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",deviceNameDictService.delInfoById(id));
        return JsonResponse.data(map);
    }


    @GetMapping("/getAllDeviceType")
    public JsonResponse getAllDeviceType(){
        HashMap<String, Object> data = new HashMap<>();
        List<DeviceTypeDict> list = deviceTypeDictService.list();
        data.put("data",list);
        return JsonResponse.data(data);
    }
}
